/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package PropertyRental.war.servlets;

import PropertyRental.ejb.persistence.Customer;
import PropertyRental.ejb.persistence.Property;
import PropertyRental.war.beans.Catalog;
import PropertyRental.war.beans.CatalogProperty;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Sébastien
 */
public class PropertyAction extends Action {
    public PropertyAction(String success, String fail) {
        super(success,fail);
    }

    @Override
    public String perform(HttpServletRequest req) {
        HttpSession session = req.getSession();
        Object account = session.getAttribute("account");
        if (account != null){
            if(account instanceof Customer){
                ArrayList<CatalogProperty> results = new ArrayList<CatalogProperty>();
                List<Property> resultList = propertyEJB.findAll();

                if (resultList != null) {
                    for (int i=0; i<resultList.size(); i++) {
                            Property x=(Property)resultList.get(i);
                            results.add(new CatalogProperty(x.getId(),x.getType(), x.getAdress(), x.getNumberBedroom(),
                                    x.getNumberBathroom(), x.getNumberOtherRoom(), x.getRent(), x.getLocation()));
                    }
                }
                req.setAttribute("title", "View Properties");
                Catalog properties = new Catalog();
                properties.setProperties(results);
                req.setAttribute("catalog", properties);

                return this.getSuccesspage();
            }
        }
        return this.getFailpage();

        
    }

}
